package com.birth.memo.service;

import java.util.ArrayList;
import java.util.List;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;

import com.birth.memo.R;
import com.birth.memo.ui.ViewMemoList;
import com.birth.memo.util.Constants;
import com.birth.memo.util.DateUtil;

public class CheckBirthService {
	
	private Context context;
	
	public CheckBirthService(Context context){
		this.context = context;
	}
	
	public void checkBirthday(){
		List<String> list = new ArrayList<String>();
		String today = DateUtil.getCurrentDate("yyyy-MM-dd");
		Cursor c = context.getContentResolver().query(Constants.URI_MEMO, null, "birth_yang=?", new String[]{today}, null);
		
		if(c.moveToFirst()){
			while(!c.isAfterLast()){
				String person = c.getString(c.getColumnIndex("person_name"));
				list.add(person);
				c.moveToNext();
			}
		}		
		String items = "";
		if(!list.isEmpty()){
			for(String personName:list){
				items = items +personName+"  ";
			}
			showNotify(context,items);
		}
	}
	
	private void showNotify(Context context,String items){
		NotificationManager nfManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
		Notification nf = new Notification();
		nf.icon = R.drawable.icon;
		nf.defaults = Notification.DEFAULT_SOUND;
		PendingIntent contentIntent = PendingIntent
				.getActivity(context, 0, new Intent(context,ViewMemoList.class), 0);
		nf.setLatestEventInfo(context, "生日提醒", items ,contentIntent);
		nf.flags = Notification.FLAG_NO_CLEAR;

		nfManager.notify(118, nf);
	}

}
